package com.hongfeng.support.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hongfeng.support.annotation.Online;
import com.hongfeng.support.common.Page;
import com.hongfeng.support.controller.common.BaseController;
import com.hongfeng.support.item.ParmItem;
import com.hongfeng.support.item.tag.FieldOperator;
import com.hongfeng.support.model.Contact;
import com.hongfeng.support.service.ContactService;
import com.hongfeng.support.util.json.JsonUtils;

/**
 * 
 * <联系方式Controller>
 * 
 * @author llc
 * @date 创建时间：2012-12-5 下午02:26:49
 */
@Controller
@Online
public class ContactController extends BaseController {

	@Resource
	private ContactService contactService;

	@RequestMapping("list")
	public void list(HttpServletRequest request, HttpServletResponse response,
			String name, Page<Contact> page) {
		log.info("联系人：" + name);
		ParmItem item = new ParmItem("name", name, FieldOperator.LIKE);
		contactService.findPageByQuery(page, Contact.class, item);
		String json = JsonUtils.bean2Json(page);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("create")
	public void create(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("contact") String contactJson) {
		log.info("添加联系方式:" + (contactJson != null ? contactJson : null));
		if (contactJson == null) {
			this.sendErrorResponse(response, "联系方式信息不能为空。");
			return;
		}
		Contact contact = (Contact) JsonUtils.json2Object(contactJson,
				Contact.class);
		ParmItem item = new ParmItem("name", contact.getName(),
				FieldOperator.EQUAL);
		List<Contact> contacts = contactService.findAllByQuery(Contact.class,
				item);
		if (contacts.size() == 0) {
			contactService.create(contact);
		} else {
			this.sendErrorResponse(response, "联系人姓名已经存在。");
		}
	}

	@RequestMapping("update")
	public void update(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("contact") String contactJson) {
		log.info("修改联系方式:" + (contactJson != null ? contactJson : null));
		if (contactJson == null) {
			this.sendErrorResponse(response, "联系方式信息不能为空。");
			return;
		}
		Contact contact = (Contact) JsonUtils.json2Object(contactJson,
				Contact.class);
		Contact qContact = contactService.read(contact.getId());
		List<Contact> contacts = null;
		if (!contact.getName().equals(qContact.getName())) {
			ParmItem item = new ParmItem("name", contact.getName(),
					FieldOperator.EQUAL);
			contacts = contactService.findAllByQuery(Contact.class, item);
		}
		if (contacts == null || contacts.size() == 0) {
			contact.setCreateTime(qContact.getCreateTime());
			contact.setCreator(qContact.getCreator());
			contactService.update(contact);
		} else {
			this.sendErrorResponse(response, "联系人姓名已经存在。");
		}
	}

	@RequestMapping("read")
	public void read(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("id") Long id) {
		log.info("读取联系方式:" + id);
		Contact contact = contactService.read(id);
		String json = JsonUtils.bean2Json(contact);
		log.info("联系方式信息：" + json);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("delete")
	public void delete(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("ids") String ids) {
		log.info("删除联系方式:" + (ids != null ? ids.toString() : null));
		if (ids == null || "".equals(ids)) {
			this.sendErrorResponse(response, "请选择要删除的联系方式。");
			return;
		}
		String[] idArr = ids.split(",");
		for (String id : idArr) {
			contactService.delete(Long.parseLong(id));
		}
	}
}
